OpenGL multiple threads, variable handling [closed]
        Posted  
        
            by 
                toeplitz
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by toeplitz
        
        
        
        Published on 2012-10-10T23:02:47Z
        Indexed on 
            2012/10/11
            9:37 UTC
        
        
        Read the original article
        Hit count: 180
        
I have written an OpenGL program which runs in the following way:
Main:
- Initialize SDL
- Create thread which has the OpenGL context:
    - Renderloop
        - Set camera (view) matrix with glUniform.
        - glDrawElements() .... etc.
        - Swapbuffers();
- Main SDL loop handling input events and such.
    - Update camera matrix of type glm::mat4. 
This is how I pass my camera object to the class that handles opengl.
Camera *cam = new Camera();
gl.setCam(cam);
where
void setCam(Camera *camera) {
    this->camera = camera;
}
For rendering in the opengl context thread, this happens:
glm::mat4 modelView = camera->view * model;
glUniformMatrix4fv(shader->bindUniform("modelView"), 1, GL_FALSE, glm::value_ptr(modelView));
In the main program where my SDL and other things are handles I then recompute the view matrix. This his working fine without me using any mutex locks. Is this correct?
On the other hand, I add objects to my scene by an "upload queue" and in this case I have to mutex lock my upload queue vector (vector class type) when adding items to it or else the program crashes.
In summary: I recompute my matrix in a different thread and then use it in the opengl thread without any mutex lock. Why is this working?
Edit: I think my question is similar to what was asked here: Should I lock a variable in one thread if I only need it's value in other threads, and why does it work if I don't?, only in my case it is even more simple with only one matrix being changed.
© Stack Overflow or respective owner